package com.tiza.xiaoyoubao.ui.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.base.BaseFragment;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.app.UIHelper;
import com.tiza.xiaoyoubao.entity.InsertTaskInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;

import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mapple on 17/4/12.
 */

public class AttentionFragment extends BaseFragment {

    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.tv4)
    TextView tv4;
    @BindView(R.id.tv5)
    TextView tv5;
    @BindView(R.id.tv6)
    TextView tv6;
    @BindView(R.id.tv7)
    TextView tv7;
    @BindView(R.id.tv8)
    TextView tv8;
    @BindView(R.id.tv9)
    TextView tv9;
    @BindView(R.id.btn_task)
    Button btn_task;
    Timer timer = new Timer();
    private int second=5;
    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);
    InsertTaskInfo mInsertTaskInfo = Hawk.get(Constants.KEY_INSERTTASK_INFO, (InsertTaskInfo) null);
    @Override
    protected int getLayoutId() {
        return R.layout.fragment_attention;
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        SpannableString spannableString1 = new SpannableString(
                mActivity.getString(R.string.text_attention1));
        SpannableString spannableString2 = new SpannableString(
                mActivity.getString(R.string.text_attention2));
        SpannableString spannableString3 = new SpannableString(
                mActivity.getString(R.string.text_attention3));
        SpannableString spannableString4 = new SpannableString(
                "确认使用"+mInsertTaskInfo.getAccount()+"接单,客户端必须使用相同账号操作,接单后2小时内完成任务");
        SpannableString spannableString5 = new SpannableString(
                mActivity.getString(R.string.text_attention5));
        SpannableString spannableString8 = new SpannableString(
                mActivity.getString(R.string.text_attention8));
        ForegroundColorSpan colorSpan = new ForegroundColorSpan(Color.parseColor("#ff3a33"));
        ForegroundColorSpan colorSpan1 = new ForegroundColorSpan(Color.parseColor("#ff3a33"));
        spannableString1.setSpan(colorSpan, 8, spannableString1.length() - 8,
                                 Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        );
        spannableString2.setSpan(colorSpan, 3, 5, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString3.setSpan(colorSpan, 0, 9, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString4.setSpan(colorSpan, 4, spannableString4.length() - 28,
                                 Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        );
        spannableString4.setSpan(colorSpan1, spannableString4.length()-8, spannableString4.length()
                - 4,
                                 Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        );
        spannableString5.setSpan(colorSpan, spannableString5.length() - 10,
                                 spannableString5.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE
        );
        spannableString8.setSpan(colorSpan, 0, 8, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);

        tv1.setText(spannableString1);
        tv2.setText(spannableString2);
        tv3.setText(spannableString3);
        tv4.setText(spannableString4);
        tv5.setText(spannableString5);
        tv8.setText(spannableString8);
        timer.schedule(task, 1000, 1000);
    }
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            mActivity.runOnUiThread(new Runnable() {      // UI thread
                @Override
                public void run() {
                    btn_task.setText("开始操作任务"+"("+second+"秒)");
                    second--;
                    if(second<0){
                        timer.cancel();
                        btn_task.setText("开始操作任务");
                        btn_task.setEnabled(true);
                        btn_task.setBackgroundResource(R.drawable.selector_comm_btn);
                    }

                }
            });
        }
    };
    @OnClick({R.id.btn_task})
    public void onCilck(View view){
        switch (view.getId()){
            case R.id.btn_task:
                Bundle args=new Bundle();
                args.putString("way","1");
                UIHelper.showOperationTask(mActivity,args);
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}
